<#assign title ="成长参数设置">
<#include "/include/layout.ftl">
<#macro css>
</#macro>
<#macro js>
</#macro>
<#macro body>
<#import "/include/select.ftl" as items>
	<section class="content-header">
      <h1>
        ${title}
        <small>添加</small>
      	<a class="btn btn-default pull-right back" href="javascript:;"><i class="fa fa-reply"></i> 返回</a>
      </h1>
    </section>
    <section class="content">
 		<div class="box box-info">
 			<div class="row">
 				<div class="col-sm-12">
			        <div class="box-header with-border">
			        	参数信息(<span style="color:red;">阶段参数一旦设置不能修改</span>)
			        </div>
			        <form action="${contextPath}/sys/grow/rule/save" method="post" id="documentForm" class="form-horizontal">
			          <input type="hidden" name="id" value="${data.id}" />
		              <div class="box-body">
		                <div class="form-group">
		                  <label class="col-sm-2 control-label">排序编号:</label>
		                  <div class="col-sm-3">
		                    <input name="orderno" value="${data.orderno}" class="form-control"  placeholder="排序编号" required digits=true max=999999 />
		                  </div>
		                </div>
		                <div class="form-group">
		                  <label class="col-sm-2 control-label">规则名称:</label>
		                  <div class="col-sm-3">
		                    <input name="rulename" value="${data.rulename}" class="form-control"  placeholder="规则名称" required maxlength="20" />
		                  </div>
		                </div>
		                <#if list?? && list?size gt 0>
		                	<#list list as stage>
		                		<div class="form-group stageform" >
				                  <label class="col-sm-2 control-label"><#if stage_index == 0>阶段:</#if></label>
				                  <div class="col-sm-3">
				                    <input value="${stage.stagename}" class="form-control stagename"  placeholder="阶段名称" readonly />
				                  </div>
				                  <div class="col-sm-3">
				                    <input value="${stage.days}"  class="form-control days" type="number"  placeholder="天数" readonly />
				                  </div>
				                  <div class="col-sm-3">
				                    <input value="${stage.consume}" class="form-control consume" type="number"  placeholder="每天消耗多少钱(单位:元)" readonly />
				                  </div>
				                  <#--  
				                  <div class="col-sm-1">
				                  	<#if stage_index == list?size - 1 >
					                  	<a class="btn btn-info pull-right stage_add" href="javascript:;"><i class="fa fa-plus-circle"></i> 添加</a>
					                  	<a class="btn btn-danger pull-right stage_del hidden" href="javascript:;"><i class="fa fa-trash"></i> 删除</a>
									<#else>		
										<a class="btn btn-danger pull-right stage_del" href="javascript:;"><i class="fa fa-trash"></i> 删除</a>		                  	
				                  	</#if>
				                  </div>
				                  -->
				                </div>
		                	</#list>
		                <#else>
			                <div class="form-group stageform" >
			                  <label class="col-sm-2 control-label">阶段:</label>
			                  <div class="col-sm-3">
			                    <input class="form-control stagename"  placeholder="阶段名称" />
			                  </div>
			                  <div class="col-sm-3">
			                    <input class="form-control days" type="number"  placeholder="天数" />
			                  </div>
			                  <div class="col-sm-3">
			                    <input class="form-control consume" type="number"  placeholder="每天消耗多少钱(单位元)" />
			                  </div>
			                  <div class="col-sm-1">
			                  	<a class="btn btn-info pull-right stage_add" href="javascript:;"><i class="fa fa-plus-circle"></i> 添加</a>
			                  	<a class="btn btn-danger pull-right stage_del hidden" href="javascript:;"><i class="fa fa-trash"></i> 删除</a>
			                  </div>
			                </div>
		                </#if>
		                <div class="form-group">
		                  <label class="col-sm-2 control-label">状态:</label>
		                  <div class="col-sm-4">
		                  	 <@items.radio name="status" list=statuslist value="${data.status!1}"  class=""  />
		                  </div>
		                </div>
		                
		              </div>
		              <div class="box-footer">
		              	<div class="col-sm-2"></div>
		              	 <div class="col-sm-6">
		              	 	<a class="btn btn-default" id="reset" ><i class="fa fa-rotate-left"></i> 重置</a>
		              	 	<a class="btn btn-info submit"><i class="fa fa-save"></i> 确定</a>
			             </div>
		              </div>
		            </form>
		             <div class="stage hidden">
			              <div class="form-group stageform">
			                  <label class="col-sm-2 control-label"></label>
			                  <div class="col-sm-3">
			                    <input class="form-control stagename"   placeholder="阶段名称" />
			                  </div>
			                  <div class="col-sm-3">
			                    <input class="form-control days" type="number"  placeholder="天数" />
			                  </div>
			                  <div class="col-sm-3">
			                    <input class="form-control consume" type="number" placeholder="每天消耗多少钱(单位元)" />
			                  </div>
			                  <div class="col-sm-1">
			                  	<a class="btn btn-info pull-right stage_add" href="javascript:;"><i class="fa fa-plus-circle"></i> 添加</a>
			                  	<a class="btn btn-danger pull-right stage_del hidden" href="javascript:;"><i class="fa fa-trash"></i> 删除</a>
			                  </div>
			                </div>
			              </div>
		              </div>
		         </div>
            </div>
        </div>
    </section>
	<script type="text/javascript">
		
		var stage = $(".stage").html();
		
		$(function(){
			//添加阶段
			$(".content").on("click",".stage_add",function(){
				$(this).hide();
				$(this).siblings().removeClass("hidden");
				$(this).parents(".form-group").after(stage);
			})
			//删除阶段
			$(".content").on("click",".stage_del",function(){
				$(this).parents(".form-group").remove();
			})
			//提交
			$(".submit").click(function(){
				//阶段参数
				var stage = [];
				var flag = true;
				$(".stageform:visible").each(function(index,element){
					var stagename = $(element).find(".stagename").val();
					var days = $(element).find(".days").val();
					var consume = $(element).find(".consume").val();
					if(stagename && days && consume){
						var json = {};
						json["stagename"] = stagename;
						json["days"] = days;
						json["consume"] = consume;
						stage.push(json);
					}else{
						flag = false;
						layer.msg("请输入完整的阶段参数!", {icon: 0});
					}
				})
				if (flag && validateForm()) {
					var options = {
							data:{"stage":util.toString(stage)},
							success: showResponse
					}
					$("#documentForm").ajaxSubmit(options);
					return false;
				}
				return false;
			})
			
		
		})
		
		//ajax 返回
		function showResponse(responseText, statusText) {
			var code = responseText.code;
			var msg = responseText.msg;
			if (code == "1") {
				  layer.alert(msg,{icon: 1}, function(index){
					  location.href="${contextPath}/sys/grow/rule/index.html";
				 	  layer.close(index);
				});  
			} else {
				layer.msg(msg, {icon: 2});
			}
		}	
		
		//表单验证
		function validateForm(){
		   return $("#documentForm").validate().form();
		}
	</script>
</#macro>